package org.luxor.common.opencv.config;

import org.luxor.common.opencv.config.properties.OpencvProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;

/**
 * opencv 全局配置
 *
 * @author Mr.Yan  @date: 2022/04/06
 */
@Configuration
@EnableConfigurationProperties(OpencvProperties.class)
@ConditionalOnProperty(prefix = OpencvProperties.PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true)
public class OpencvAutoConfig {
    private static final Logger logger = LoggerFactory.getLogger(OpencvAutoConfig.class);

    public OpencvAutoConfig(OpencvProperties opencvProperties) {
        logger.info("opencv Initialization configuration.  Started...");
        Assert.notNull(opencvProperties, "Spring容器中找不到[opencvProperties]的实例化对象!");

        try {
            // 加载动态库
            logger.info(" load opencv Library: [{}].", opencvProperties.getLibraryPath());
            System.load(opencvProperties.getLibraryPath());
        } catch (Throwable e) {
            logger.error(" Can't load opencv Library: [{}]", opencvProperties.getLibraryPath());
            throw e;
        }

        // 解决awt报错问题
        System.setProperty("java.awt.headless", "false");

        logger.info("opencv Initialization configuration, completed.");
    }

}
